
package tools.data.output;



import tools.HexTool;

import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;















 public class GenericLittleEndianWriter
         implements LittleEndianWriter
         {
    /*  20 */   private static final Charset ASCII = Charset.forName("GBK");




       private ByteOutputStream bos;






    protected GenericLittleEndianWriter() {
    }






    public GenericLittleEndianWriter(ByteOutputStream bos) {
        /*  36 */
        this.bos = bos;

    }








    protected void setByteOutputStream(ByteOutputStream bos) {
        /*  45 */
        this.bos = bos;

    }







    public final void writeZeroBytes(int i) {
        /*  53 */
        for (int x = 0; x < i; x++) {
            /*  54 */
            this.bos.writeByte((byte) 0);

        }

    }









    public final void write(byte[] b) {
        /*  65 */
        for (byte aB : b) {
            /*  66 */
            this.bos.writeByte(aB);

        }

    }









    public final void write(byte b) {
        /*  77 */
        this.bos.writeByte(b);

    }









    public final void write(int b) {
        /*  87 */
        this.bos.writeByte((byte) b);

    }









    public final void writeShort(short i) {
        /*  97 */
        this.bos.writeByte((byte) (i & 0xFF));
        /*  98 */
        this.bos.writeByte((byte) (i >>> 8 & 0xFF));

    }









    public final void writeShort(int i) {
        /* 108 */
        this.bos.writeByte((byte) (i & 0xFF));
        /* 109 */
        this.bos.writeByte((byte) (i >>> 8 & 0xFF));

    }









    public final void writeInt(int i) {
        /* 119 */
        this.bos.writeByte((byte) (i & 0xFF));
        /* 120 */
        this.bos.writeByte((byte) (i >>> 8 & 0xFF));
        /* 121 */
        this.bos.writeByte((byte) (i >>> 16 & 0xFF));
        /* 122 */
        this.bos.writeByte((byte) (i >>> 24 & 0xFF));

    }




    public final void writeReversedInt(long l) {
        /* 127 */
        this.bos.writeByte((byte) (int) (l >>> 32L & 0xFFL));
        /* 128 */
        this.bos.writeByte((byte) (int) (l >>> 40L & 0xFFL));
        /* 129 */
        this.bos.writeByte((byte) (int) (l >>> 48L & 0xFFL));
        /* 130 */
        this.bos.writeByte((byte) (int) (l >>> 56L & 0xFFL));

    }









    public final void writeAsciiString(String s) {
        /* 140 */
        write(s.getBytes(ASCII));

    }










    public final void writeAsciiString(String s, int max) {
        /* 151 */
        if ((s.getBytes(ASCII)).length > max) {

            try {
                /* 153 */
                s = 取指定长度字符串(s, "gbk", max);
                /* 154 */
            } catch (Exception exception) {
            }

        }




        /* 160 */
        write(s.getBytes(ASCII));
        /* 161 */
        for (int i = (s.getBytes(ASCII)).length; i < max; i++) {
            /* 162 */
            write(0);

        }

    }









    public final void writeMapleNameString(String s) {
        /* 173 */
        if ((s.getBytes(ASCII)).length > 13) {

            try {
                /* 175 */
                s = 取指定长度字符串(s, "gbk", 13);
                /* 176 */
            } catch (Exception exception) {
            }

        }




        /* 182 */
        write(s.getBytes(ASCII));
        /* 183 */
        for (int i = (s.getBytes(ASCII)).length; i < 13; i++) {
            /* 184 */
            write(0);

        }

    }









    public final void writeMapleAsciiString(String s) {
        /* 195 */
        if (s == null) {
            /* 196 */
            writeShort(0);

            return;

        }
        /* 199 */
        writeShort((short) (s.getBytes(ASCII)).length);
        /* 200 */
        writeAsciiString(s);

    }




    public final void writeMapleAsciiString(String s, int max) {
        /* 205 */
        writeShort((short) max);
        /* 206 */
        if ((s.getBytes()).length > max) {
            /* 207 */
            s = HexTool.getSubstring(s, 1, max);

        }
        /* 209 */
        writeAsciiString(s);
        /* 210 */
        for (int x = (s.getBytes(ASCII)).length; x < max; x++) {
            /* 211 */
            write(0);

        }

    }



    public void writeMapleAsciiString(String[] arrstring) {
        /* 216 */
        int n2 = 0;
        /* 217 */
        for (String string : arrstring) {
            /* 218 */
            if (string != null) {
                /* 219 */
                n2 += (string.getBytes(ASCII)).length;

            }

        }
        /* 222 */
        if (n2 < 1) {
            /* 223 */
            writeShort(0);

            return;

        }
        /* 226 */
        writeShort((short) (n2 + arrstring.length - 1));
        /* 227 */
        for (int i = 0; i < arrstring.length; i++) {
            /* 228 */
            if (arrstring[i] != null) {
                /* 229 */
                writeAsciiString(arrstring[i]);

            }
            /* 231 */
            if (i < arrstring.length - 1) {
                /* 232 */
                write(0);

            }

        }

    }









    public final void writePos(Point s) {
        /* 244 */
        writeShort(s.x);
        /* 245 */
        writeShort(s.y);

    }









    public final void writeRect(Rectangle s) {
        /* 255 */
        writeInt(s.x);
        /* 256 */
        writeInt(s.y);
        /* 257 */
        writeInt(s.x + s.width);
        /* 258 */
        writeInt(s.y + s.height);

    }









    public final void writeLong(long l) {
        /* 268 */
        this.bos.writeByte((byte) (int) (l & 0xFFL));
        /* 269 */
        this.bos.writeByte((byte) (int) (l >>> 8L & 0xFFL));
        /* 270 */
        this.bos.writeByte((byte) (int) (l >>> 16L & 0xFFL));
        /* 271 */
        this.bos.writeByte((byte) (int) (l >>> 24L & 0xFFL));
        /* 272 */
        this.bos.writeByte((byte) (int) (l >>> 32L & 0xFFL));
        /* 273 */
        this.bos.writeByte((byte) (int) (l >>> 40L & 0xFFL));
        /* 274 */
        this.bos.writeByte((byte) (int) (l >>> 48L & 0xFFL));
        /* 275 */
        this.bos.writeByte((byte) (int) (l >>> 56L & 0xFFL));

    }




    public final void writeReversedLong(long l) {
        /* 280 */
        this.bos.writeByte((byte) (int) (l >>> 32L & 0xFFL));
        /* 281 */
        this.bos.writeByte((byte) (int) (l >>> 40L & 0xFFL));
        /* 282 */
        this.bos.writeByte((byte) (int) (l >>> 48L & 0xFFL));
        /* 283 */
        this.bos.writeByte((byte) (int) (l >>> 56L & 0xFFL));
        /* 284 */
        this.bos.writeByte((byte) (int) (l & 0xFFL));
        /* 285 */
        this.bos.writeByte((byte) (int) (l >>> 8L & 0xFFL));
        /* 286 */
        this.bos.writeByte((byte) (int) (l >>> 16L & 0xFFL));
        /* 287 */
        this.bos.writeByte((byte) (int) (l >>> 24L & 0xFFL));

    }









    public final void writeBool(boolean b) {
        /* 297 */
        write(b ? 1 : 0);

    }









    public final void writeReversedBool(boolean b) {
        /* 307 */
        write(b ? 0 : 1);

    }




    @Override
    public final void writeHexString(final String s) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int nexti = 0;
        int nextb = 0;
        boolean highoc = true;
        outer:
        for (; ; ) {
            int number = -1;
            while (number == -1) {
                if (nexti == s.length()) {
                    break outer;
                }
                char chr = s.charAt(nexti);
                if (chr >= '0' && chr <= '9') {
                    number = chr - '0';
                } else if (chr >= 'a' && chr <= 'f') {
                    number = chr - 'a' + 10;
                } else if (chr >= 'A' && chr <= 'F') {
                    number = chr - 'A' + 10;
                } else {
                    number = -1;
                }
                nexti++;
            }
            if (highoc) {
                nextb = number << 4;
                highoc = false;
            } else {
                nextb |= number;
                highoc = true;
                baos.write(nextb);
            }
        }
        write(baos.toByteArray());
    }




    public void writePosAsInt(Point s) {
        /* 349 */
        writeInt(s.x);
        /* 350 */
        writeInt(s.y);

    }






    private static String 取指定长度字符串(String originString, String charsetName, int byteLen) throws UnsupportedEncodingException {
        /* 357 */
        if (originString == null || originString.isEmpty() || byteLen <= 0) {
            /* 358 */
            return "";

        }
        /* 360 */
        char[] chars = originString.toCharArray();
        /* 361 */
        int length = 0, index = chars.length;
        /* 362 */
        for (int i = 0; i < chars.length; i++) {
            /* 363 */
            int len = (String.valueOf(chars[i]).getBytes(charsetName)).length + length;
            /* 364 */
            if (len <= byteLen) {
                /* 365 */
                length = len;

            } else {
                /* 367 */
                index = i;

                break;

            }

        }
        /* 371 */
        return String.valueOf(chars, 0, index);

    }

}


/* Location:              C:\PGX\Server.jar!\tools\data\output\GenericLittleEndianWriter.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */